package com.suray.rgv60.generate;

import com.alibaba.fastjson.JSON;
import com.suray.basic.wcs.path.core.Node;
import com.suray.basic.wcs.utils.WcsFuncUtil;
import com.suray.type.RgvMapTagDirection;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 生成地图处理
 */
public class GeneralMapManage {
    public static void generalMap( String paperFile ,int floor, String version, List<Node> mapNodes,
                                  RgvMapTagDirection direction, List<Rgv60MapStruct.Scope> advancedAreaList)
            throws IOException {
        String name = "seer_" + floor + version;
        //创建文件夹
        String filename = paperFile + File.separator + "wcs_map" + File.separator + name + ".smap";
        WcsFuncUtil.createPaper(filename);
        File file = new File(filename);
        FileOutputStream fop = new FileOutputStream(file);
        //带小车方向的新版地图
        fop.write(JSON.toJSONString(new Rgv60MapStruct(name, mapNodes, direction, advancedAreaList)).getBytes());
        fop.flush();
        fop.close();
    }

    public static List<File> getMapFiles(String paperFile) {
        List<File> files = new ArrayList<>();
        File parent = new File(paperFile);

        if (parent.listFiles() == null) {
            return new ArrayList<>();
        }

        for (File file : parent.listFiles()) {
            files.add(file);
        }

        return files;
    }

    /**
     * 获取地图数据对象
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static Rgv60MapStruct generateRgv60MapStruct(InputStream inputStream) throws IOException {
        String agvMapFileContext = getSmapJsonData(inputStream);
        return JSON.parseObject(agvMapFileContext,Rgv60MapStruct.class);
    }

    /**
     * 读取地图文件内容
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static String getSmapJsonData(InputStream inputStream) throws IOException {
        Reader reader = new InputStreamReader(inputStream, "Utf-8");
        int ch = 0;
        StringBuffer sb = new StringBuffer();
        while ((ch = reader.read()) != -1) {
            sb.append((char) ch);
        }
        reader.close();
        return sb.toString();
    }
}
